[pigeon] updates toString and isNullish methods#11625
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates Pigeon to version 26.4.0, introducing automatic generation of toString (or equivalent) methods for data classes across all supported languages. It also refines the Swift isNullish utility to handle nested optional values. Review feedback highlights a critical logic error in the C++ generator that results in unbalanced braces and corrupted code, along with a compilation issue when handling C++ enums. Additionally, for Swift, the feedback identifies a redundant protocol conformance error in subclasses and suggests using String(describing:) to prevent compiler warnings during string interpolation of optional fields.
| indent.writeln('ss << *$name;'); | ||
| } | ||
| }); | ||
| indent.nest(1, () { | ||
| indent.writeln('ss << "null";'); | ||
| }); | ||
| } else { | ||
| if (field.type.isClass) { | ||
| indent.writeln('ss << $name.ToString();'); | ||
| } else { | ||
| indent.writeln('ss << $name;'); |
There was a problem hiding this comment.
There was a problem hiding this comment.
Gemini is correct here. You'll also need custom handling of maps, and maybe lists.
| final Iterable<String> fieldStrings = classDefinition.fields.map(( | ||
| NamedType field, | ||
| ) { | ||
| return '${field.name}: \\(${field.name})'; |
There was a problem hiding this comment.
In Swift, string interpolation of optional values triggers a compiler warning (e.g., "Expression implicitly coerced from 'String?' to 'Any'"). It is recommended to wrap optional fields in String(describing:) to silence this warning and provide a consistent string representation.
| return '${field.name}: \\(${field.name})'; | |
| return '${field.name}: \(String(describing: ${field.name}))'; |
stuartmorgan-g
left a comment
There was a problem hiding this comment.
The native unit tests should have a test that calls the to-string method for a test object and ensure that the expected string results, so that we are validating that the methods do what we expect (and also have a clear snapshot of what the expected output actually is).
| indent.writeln('ss << *$name;'); | ||
| } | ||
| }); | ||
| indent.nest(1, () { | ||
| indent.writeln('ss << "null";'); | ||
| }); | ||
| } else { | ||
| if (field.type.isClass) { | ||
| indent.writeln('ss << $name.ToString();'); | ||
| } else { | ||
| indent.writeln('ss << $name;'); |
There was a problem hiding this comment.
Gemini is correct here. You'll also need custom handling of maps, and maybe lists.
| NamedType field, | ||
| ) { | ||
| if (_usesPrimitive(field.type)) { | ||
| return '@(self.${field.name})'; |
There was a problem hiding this comment.
FWIW it would be conceptually cleaner to use the right format string for each primitive type, rather than boxing them just to print them as objects. But since this is just a nice-to-have utility method, and Obj-C isn't a development focus, it's fine to leave as-is.
There was a problem hiding this comment.
lmk if my change is what you had in mind.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request implements toString (or equivalent) method generation for data classes across all supported Pigeon generators, including Dart, Java, Kotlin, Swift, Objective-C, C++, and GObject. It also updates the Swift isNullish helper to correctly handle double-nested Any? values and adds new list-based echo methods to the core integration tests. Feedback identifies a missing nullability check for boolean fields in the GObject generator and potential integer truncation issues in the Objective-C description implementation on 32-bit platforms. Additionally, a suggestion was made to maintain field name consistency in GObject string representations.
| } else if (field.type.baseName == 'bool') { | ||
| indent.writeln( | ||
| 'g_string_append(str, self->$fieldName ? "true" : "false");', | ||
| ); |
There was a problem hiding this comment.
The bool case is missing nullability handling. In the GObject generator, nullable booleans are represented as gboolean*. Using self->fieldName directly in a ternary will check the pointer's validity rather than the boolean value, and it will print "false" instead of "null" when the pointer is null.
} else if (field.type.baseName == 'bool') {
if (field.type.isNullable) {
indent.writeScoped('if (self->$fieldName != nullptr) {', '}', () {
indent.writeln(
'g_string_append(str, *self->$fieldName ? "true" : "false");',
);
});
indent.writeScoped('else {', '}', () {
indent.writeln('g_string_append(str, "null");');
});
} else {
indent.writeln(
'g_string_append(str, self->$fieldName ? "true" : "false");',
);
}
}| case 'bool': | ||
| return '${field.name}: %@'; | ||
| case 'int': | ||
| return '${field.name}: %ld'; |
There was a problem hiding this comment.
Pigeon int fields are generated as int64_t in Objective-C. Using %ld with a (long) cast can lead to integer truncation on 32-bit platforms. It is safer to use %lld with a (long long) cast to ensure full 64-bit precision is maintained in the description.
| return '${field.name}: %ld'; | |
| case 'int': | |
| return '${field.name}: %lld'; |
| case 'bool': | ||
| return 'self.${field.name} ? @"true" : @"false"'; | ||
| case 'int': | ||
| return '(long)self.${field.name}'; |
| enumerate(classDefinition.fields, (int index, final NamedType field) { | ||
| final String fieldName = _getFieldName(field.name); | ||
| final comma = index == 0 ? '' : ', '; | ||
| indent.writeln('g_string_append(str, "$comma$fieldName: ");'); |
There was a problem hiding this comment.
For consistency with other generated platforms (Dart, Java, Kotlin, Swift, C++), the label in the string representation should use the original field name (field.name) rather than the snake_case GObject member name (fieldName).
| indent.writeln('g_string_append(str, "$comma$fieldName: ");'); | |
| indent.writeln('g_string_append(str, "$comma${field.name}: ");'); |
prequel pr to #11352 to land non NI changes to simplify pr.